package com.cai310.lottery.entity.user;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.commons.lang.StringUtils;

import com.cai310.entity.CreateMarkable;
import com.cai310.entity.IdEntity;
import com.cai310.entity.UpdateMarkable;
import com.cai310.lottery.common.AgentLotteryType;
import com.google.common.collect.Lists;

@Entity
@Table(name = com.cai310.lottery.Constant.LOTTERY_TABLE_PREFIX + "AGENT_LINK")
public class AgentLink extends IdEntity implements CreateMarkable, UpdateMarkable, Serializable {
	private static final long serialVersionUID = 6420283302225215768L;
	private Long userId;
	private String userRebate;//EL11TO5/PL3D/NUMBER/DCZC/ZC/JC/KENO
	private Date createTime;

	/** 最后更新时间 */
	private Date lastModifyTime;
	/** 版本号,用于实现乐观锁 */
	private Integer version;

	// Constructors

	/** default constructor */
	public AgentLink() {
	}
	@Transient
	public List<UserRebate> getUserRebateList(){
		List<UserRebate> list = Lists.newArrayList();
		if(StringUtils.isNotBlank(this.getUserRebate())){
			String[] arr = this.getUserRebate().split(",");
			for (int i = 0; i < arr.length; i++) {
				UserRebate userRebate = new UserRebate();
				userRebate.setAgentLotteryType(AgentLotteryType.values()[i]);
				userRebate.setRebate(Double.valueOf(arr[i]));
				list.add(userRebate);
			}
			
		}
		return list;
	}
	@Transient
	public void setUserRebateList(List<UserRebate> list){
		NumberFormat TWO_NF = new DecimalFormat("0.0");
		String[] arr = new String[list.size()];
		for (UserRebate userRebate : list) {
			int ordinal = userRebate.getAgentLotteryType().ordinal();
			arr[ordinal] = TWO_NF.format(userRebate.getRebate());
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			sb.append(arr[i]).append(",");
		}
		this.setUserRebate(sb.delete(sb.length()-1, sb.length()).toString());
	}
	/**
	 * @return the createTime
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, updatable = false)
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(insertable = false)
	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}
	/**
	 * @return {@link #version}
	 */
	@Version
	@Column(nullable = false)
	public Integer getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}
	@Column
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	@Column
	public String getUserRebate() {
		return userRebate;
	}
	public void setUserRebate(String userRebate) {
		this.userRebate = userRebate;
	}
	


}